package cn.wolfcode.car.business.service.impl;

import cn.wolfcode.car.business.domain.Statement;
import cn.wolfcode.car.business.domain.StatementItem;
import cn.wolfcode.car.business.mapper.StatementItemMapper;
import cn.wolfcode.car.business.query.StatementItemQuery;
import cn.wolfcode.car.business.service.IStatementItemService;
import cn.wolfcode.car.business.service.IStatementService;
import cn.wolfcode.car.common.base.page.TablePageInfo;
import cn.wolfcode.car.common.exception.BusinessException;
import cn.wolfcode.car.common.util.Convert;
import com.github.pagehelper.PageHelper;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import java.math.BigDecimal;
import java.util.List;

@Service
@Transactional
public class StatementItemServiceImpl implements IStatementItemService {

    @Autowired
    private StatementItemMapper statementItemMapper;
    @Autowired
    private IStatementService statementService;

    @Override
    public TablePageInfo<StatementItem> query(StatementItemQuery qo) {
        PageHelper.startPage(qo.getPageNum(), qo.getPageSize());
        return new TablePageInfo<StatementItem>(statementItemMapper.selectForList(qo));
    }


    @Override
    public void save(StatementItem statementItem) {
        statementItemMapper.insert(statementItem);
    }

    @Override
    public StatementItem get(Long id) {
        return statementItemMapper.selectByPrimaryKey(id);
    }


    @Override
    public void update(StatementItem statementItem) {
        statementItemMapper.updateByPrimaryKey(statementItem);
    }

    @Override
    public void deleteBatch(String ids) {
        Long[] dictIds = Convert.toLongArray(ids);
        for (Long dictId : dictIds) {
            statementItemMapper.deleteByPrimaryKey(dictId);
        }
    }

    @Override
    @Transactional
    public void saveItem(List<StatementItem> list) {
        //取出最后一条数据，获取额外信息
        StatementItem temp = list.remove(list.size() - 1);
        BigDecimal discountAmount = temp.getItemPrice();
        Long statementId = temp.getStatementId();
        //处于消费的记录才能进行保存明细操作
        Statement statement = statementService.get(statementId);
        if (Statement.STATUS_PAID.equals(statement.getStatus())) {
            throw new BusinessException("已支付的结算单不能进行保存操作");
        }
        //删除明细操作
        statementService.deleteItemByStatementId(statementId);

        //统计明细的总余额
        BigDecimal totalAmount = new BigDecimal("0.00"); //总消费金额
        BigDecimal totalQuantity = new BigDecimal("0");//服务项数量

        //统计明细的数量
        //保存结算单的明细
        for (StatementItem item : list) {
            statementItemMapper.insert(item);
            //总金额 = （明细单件 * 数量）
            totalAmount = totalAmount.add(item.getItemPrice().multiply(item.getItemQuantity()));
            totalQuantity = totalQuantity.add(item.getItemQuantity());
        }
        //优惠金额不能大于总金额
        if (discountAmount.compareTo(totalAmount)>0){
            throw new BusinessException("非法操作");
        }
        //更新结算单信息
        statement.setTotalAmount(totalAmount);
        statement.setTotalQuantity(totalQuantity);
        statement.setDiscountAmount(discountAmount);
        statementService.updateByStatementItem(statement);

    }

    @Override
    public List<StatementItem> list() {
        return statementItemMapper.selectAll();
    }

}











